package com.sea.base.ext.obs

import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.sea.base.ext.global.postMain

/**
 * 由于obs默认保留前一次数据，这个方法屏蔽前一次消息只会收到之后的
 */
@MainThread
inline fun Lifecycle.addAfterObserver(
    crossinline callback: LifecycleEventObserver.(owner: LifecycleOwner, event: Lifecycle.Event) -> Unit
): LifecycleEventObserver {
    val obs = object : LifecycleEventObserver {
        var init = false
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            if (!init) {
                //忽略之前的
                return
            }
            callback.invoke(this, source, event)
        }
    }
    addObserver(obs)
    obs.init = true
    return obs
}

/**
 * kotlin版addObserver
 */
inline fun Lifecycle.addObserver(
    crossinline callback: LifecycleEventObserver.(owner: LifecycleOwner, event: Lifecycle.Event) -> Unit
): LifecycleEventObserver {
    val obs = lifecycleEventObserver(callback)
    addObserver(obs)
    return obs
}

/**
 * 带this的inline版实现
 */
inline fun lifecycleEventObserver(
    crossinline callback: LifecycleEventObserver.(source: LifecycleOwner, event: Lifecycle.Event) -> Unit
): LifecycleEventObserver {
    return object : LifecycleEventObserver {
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            callback.invoke(this, source, event)
        }
    }
}

/**
 * create之后执行，只执行一次
 */
fun Lifecycle.doOnCreated(callback: Runnable): LifecycleEventObserver? {
    if (currentState.isAtLeast(Lifecycle.State.CREATED)) {
        //本来就是create
        callback.run()
        return null
    }
    return addObserver { _, event ->
        when {
            event.targetState.isAtLeast(Lifecycle.State.CREATED) -> {
                //回调是在create之前，所以要post一下
                postMain(run = callback)
                removeObserver(this)
            }

            event == Lifecycle.Event.ON_DESTROY -> {
                removeObserver(this)
            }
        }
    }
}

/**
 * 执行在resume时（如果已经resume过会立即执行）
 */
fun Lifecycle.doOnResumed(
    callback: Runnable
): LifecycleEventObserver {
    return addObserver { _, event ->
        when (event) {
            Lifecycle.Event.ON_RESUME -> {
                //回调是在resume之前，所以要post一下
                postMain(run = callback)
                removeObserver(this)
            }

            Lifecycle.Event.ON_DESTROY -> {
                removeObserver(this)
            }

            else -> {
                //暂时没逻辑
            }
        }
    }
}

/**
 * 执行在下一次resume后（忽略之前的resume）
 */
fun Lifecycle.doOnNextResumed(
    callback: Runnable
): LifecycleEventObserver {
    return addAfterObserver { _, event ->
        when (event) {
            Lifecycle.Event.ON_RESUME -> {
                //回调是在resume之前，所以要post一下
                postMain(run = callback)
                removeObserver(this)
            }

            Lifecycle.Event.ON_DESTROY -> {
                removeObserver(this)
            }

            else -> {
                //暂时没逻辑
            }
        }
    }
}

fun Lifecycle.doOnDestroyed(callback: Runnable): LifecycleEventObserver? {
    if (currentState == Lifecycle.State.DESTROYED) {
        callback.run()
        return null
    }
    return addObserver { _, event ->
        if (event == Lifecycle.Event.ON_DESTROY) {
            callback.run()
            removeObserver(this)
        }
    }
}